namespace CSharpPro._1013;

public class Demo4
{
    //1013作业一
    public void TestPrint1(int x, int y, int z)
    {
        int[] numbers = { x, y, z };
        Array.Sort(numbers);
        Console.WriteLine("从小到大排序结果：");
        Console.WriteLine($"{numbers[0]} < {numbers[1]} < {numbers[2]}");
    }

    //1013作业二
    public void TestPrint2()
    {
        int sum = 0;
        List<int> validNumbers = new List<int>();

        for (int i = 1; i <= 100; i++)
        {
            bool isMultipleOf7 = i % 7 == 0;
            bool contains7 = i % 10 == 7 || i / 10 == 7;
            if (!isMultipleOf7 && !contains7)
            {
                validNumbers.Add(i);
                sum += i;
            }
        }

        Console.WriteLine("1-100之间所有不是7的倍数且不包含7的数字：");

        for (int i = 0; i < validNumbers.Count; i++)
        {
            Console.Write(validNumbers[i] + " ");
            if ((i + 1) % 10 == 0)
            {
                Console.WriteLine();
            }
        }

        Console.WriteLine($"\n这些数字的总和是：{sum}");
    }

    //1013作业三
    public void TestPrint3()
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}×{i}={i * j}\t");
            }

            Console.WriteLine();
        }
    }

    //1013作业四
    public void TestPrint4()
    {
        Console.WriteLine("1000以内的完数有：");
        for (int num = 2; num <= 1000; num++)
        {
            List<int> factors = GetProperFactors(num);

            int sum = 0;
            foreach (int factor in factors)
            {
                sum += factor;
            }

            if (sum == num)
            {
                Console.Write($"{num} (因子：");
                Console.Write(string.Join("+", factors));
                Console.WriteLine($") = {sum}");
            }
        }
    }

    private List<int> GetProperFactors(int number)
    {
        List<int> factors = new List<int>();

        if (number > 1)
        {
            factors.Add(1);
        }

        for (int i = 2; i <= number / 2; i++)
        {
            if (number % i == 0)
            {
                factors.Add(i);
            }
        }

        return factors;
    }

    //1013作业五
    public void TestPrint5(int number)
    {
        int originalNumber = number;
        int digitCount = 0;
        string reversedDigits = "";

        while (number > 0)
        {
            int lastDigit = number % 10;
            reversedDigits += lastDigit.ToString() + " ";

            number = number / 10;

            digitCount++;
        }

        Console.WriteLine($"{originalNumber} 是 {digitCount} 位数");
        Console.WriteLine("逆序打印各位数字：" + reversedDigits.TrimEnd());
    }

    //1013作业六
    public void TestPrint6(int rows)
    {
        if (rows % 2 == 0)
        {
            Console.WriteLine("请输入奇数行数以获得对称的菱形");
            return;
        }

        int half = rows / 2;
        for (int i = 0; i <= half; i++)
        {
            for (int j = 0; j < half - i; j++)
            {
                Console.Write(" ");
            }

            for (int k = 0; k < 2 * i + 1; k++)
            {
                Console.Write("*");
            }

            Console.WriteLine();
        }

        for (int i = half - 1; i >= 0; i--)
        {
            for (int j = 0; j < half - i; j++)
            {
                Console.Write(" ");
            }

            for (int k = 0; k < 2 * i + 1; k++)
            {
                Console.Write("*");
            }

            Console.WriteLine();
        }
    }
}